package btest;

import java.util.Random;

/**
 * AdorynZhao in Toutiao-UESTC.
 * Source Code Created on 2017/12/26.
 */
public class TimeTest {

    public long start = 0;
    public long end = 0;

    public static void main(String[] args) {
        TimeTest tt = new TimeTest();
        tt.start = System.currentTimeMillis();

        Process p1 = new Process(tt);
        new Thread(p1).start();

        Process p2 = new Process(tt);
        new Thread(p2).start();

        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Time Duration = " + ((double)(tt.end - tt.start) / 1000));
    }

    public static class Process implements Runnable {

        public TimeTest tt = null;

        public Process(TimeTest tt) {
            this.tt = tt;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(new Random().nextInt(4) * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (tt) {
                tt.end = System.currentTimeMillis();
                System.out.println(Thread.currentThread() + " End = " + ((double)(tt.end - tt.start) / 1000));
            }
        }
    }
}